<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\company;

use app\model\mysql\company\DepartmentModel;
use app\service\BaseService;

final class DepartmentService extends BaseService
{
    /**
     * @var array 部门区域
     */
    protected static array $departmentArea = [];

    /**
     * 获取缓存中部门区域值
     * @param int $id 部门信息
     * @return int
     */
    public static function getAreaByCache(int $id = 0): int
    {
        if (! isset(self::$departmentArea[$id])) {
            self::$departmentArea[$id] = self::getArea($id);
        }
        return intval(self::$departmentArea[$id]);
    }

    /**
     * 实时获取部门区域值
     * @param int $id 部门信息
     * @return int
     */
    public static function getArea(int $id = 0): int
    {
        return intval((new DepartmentModel())->where('id', '=', $id)->value('area', 0));
    }
}
